package src.com.neusoft.nep.controller;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import src.com.neusoft.nep.common.AlertUtils;
import src.com.neusoft.nep.common.SwitchScene;
import src.com.neusoft.nep.entity.Nepg;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class NepgLoginViewController {
    @FXML
    private TextField txt_nepgCode;
    @FXML
    private PasswordField psw_nepgPassword;
    @FXML
    private Button btn_nepgLogin;
    @FXML
    private Button btn_back;

    @FXML
    private ImageView imageView;
    @FXML
    AnchorPane anchorPane;
    @FXML
    StackPane stackPane;

    private Stage currentStage;
    private static NepgLoginViewController instance; // 单例实例
    private String loggedInCode; // 保存登录的code

    public void setLoginViewStage(Stage stage) {
        this.currentStage = stage;
    }
    // 初始化时设置单例实例
    @FXML
    public void instancenitialize() {
        instance = this;
    }
    // 获取单例实例
    public static NepgLoginViewController getInstance() {
        return instance;
    }
    // 获取登录的code
    public String getLoggedInCode() {
        return loggedInCode;
    }

    public void login() throws IOException {
        Nepg n1 = new Nepg("11", "11");
        Nepg n2 = new Nepg("22", "22");
        Nepg n3 = new Nepg("33", "33");

        String nepgCode = txt_nepgCode.getText();
        String nepgPassword = psw_nepgPassword.getText();

        Nepg nLogin = new Nepg(nepgCode, nepgPassword);

        Map<String, Nepg> nepgMap = new HashMap<>();
        nepgMap.put(n1.getCode(), n1);
        nepgMap.put(n2.getCode(), n2);
        nepgMap.put(n3.getCode(), n3);

        try {
            if (nepgMap.containsKey(nLogin.getCode())) {
                if (nepgPassword.equals(nepgMap.get(nepgCode).getPassword())) {
                    AlertUtils.showDialog("", "登录成功");
                    loggedInCode = nepgCode; // 保存登录的code
                    SwitchScene.switchScene(btn_nepgLogin, "../view/NepgTaskManageView.fxml");
                } else {
                    AlertUtils.showDialog("", "密码错误");
                    return;
                }
            } else {
                AlertUtils.showDialog("", "用户不存在");
                return;
            }

        } finally {
        }
    }

    @FXML
    public void initialize() {
        instancenitialize();

        Image image = new Image("/src/com/neusoft/nep/image/Nepg01.png");
        imageView.setImage(image);
        imageView.fitHeightProperty().bind(anchorPane.heightProperty());
        imageView.fitWidthProperty().bind(anchorPane.widthProperty());
        imageView.setPreserveRatio(false);
        imageView.setSmooth(true);
    }

    @FXML
    public void back() {
        // 获取当前舞台
        Stage currentStage = (Stage) btn_back.getScene().getWindow();
        // 关闭当前舞台
        currentStage.close();
    }
}
